package com.serviceprovider.business.module.common;

//~--- non-JDK imports --------------------------------------------------------

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;

//~--- JDK imports ------------------------------------------------------------

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.List;

public class PaginationHelper<E> {
    public Page<E> fetchPage(final JdbcTemplate jt, final String sqlCountRows, final String sqlFetchRows,
                             final Object args[], final int pageNo, final int recordSize, final String sortColumn,
                             final String sortOrder, final ParameterizedRowMapper<E> rowMapper) {

        // determine how many rows are available
        final int rowCount = jt.queryForInt(sqlCountRows, args);

        // calculate the number of pages
        int pageCount = rowCount / recordSize;

        if (rowCount > recordSize * pageCount) {
            pageCount++;
        }

        // create the page object
        final Page<E> page = new Page<E>();

        page.setPageNumber(pageNo);
        page.setPagesAvailable(pageCount);
        page.setTotalRecords(rowCount);
        page.setRecordSize(recordSize);
        page.setSortColumn(sortColumn);
        page.setSortOrder(sortOrder);

        // fetch a single page of results
        final int startRow = (pageNo - 1) * recordSize;

        jt.query(sqlFetchRows, args, new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
                final List pageItems  = page.getPageItems();
                int        currentRow = 0;

                while (rs.next() && (currentRow < startRow + recordSize)) {
                    if (currentRow >= startRow) {
                        pageItems.add(rowMapper.mapRow(rs, currentRow));
                    }

                    currentRow++;
                }

                return page;
            }
        });

        return page;
    }

    public Page<E> fetchPage(final JdbcTemplate jt, final String sqlCountRows, final String sqlFetchRows,
                             final Object args[], final int pageNo, final int recordSize,
                             final ParameterizedRowMapper<E> rowMapper) {

        // determine how many rows are available
        final int rowCount = jt.queryForInt(sqlCountRows, args);

        // calculate the number of pages
        int pageCount = rowCount / recordSize;

        if (rowCount > recordSize * pageCount) {
            pageCount++;
        }

        // create the page object
        final Page<E> page = new Page<E>();

        page.setPageNumber(pageNo);
        page.setPagesAvailable(pageCount);
        page.setTotalRecords(rowCount);
        page.setRecordSize(recordSize);

        // fetch a single page of results
        final int startRow = (pageNo - 1) * recordSize;

        jt.query(sqlFetchRows, args, new ResultSetExtractor() {
            public Object extractData(ResultSet rs) throws SQLException, DataAccessException {
                final List pageItems  = page.getPageItems();
                int        currentRow = 0;

                while (rs.next() && (currentRow < startRow + recordSize)) {
                    if (currentRow >= startRow) {
                        pageItems.add(rowMapper.mapRow(rs, currentRow));
                    }

                    currentRow++;
                }

                return page;
            }
        });

        return page;
    }
}



